<?php
	// Uninstall JaJobBoard component
	jimport('joomla.installer.installer');
	jimport('joomla.installer.helper');
	jimport('joomla.filesystem.file');
	jimport('joomla.filesystem.folder');
	$db = JFactory::getDBO();
	$messages = array();

	$arrPackages = array("mod_jalist_jobs");

	foreach ($arrPackages as $package){
		$type = substr($package, 0, 3);
		switch ($type){
			case "mod":{
				deleteFolder("modules".DS.$package);
				deleteDatabase("#__modules", "`module` = '".$package."'");
				$messages[] = JText::_("Uninstall module: ".$package." successfully");
				break;
			}
			case "plg":{
				$info = explode("_", $package);
				if( count($info) >= 3 ){
					$info[2] = str_replace($info[0]."_".$info[1]."_", "", $package);
					$db->setQuery("SELECT * FROM #__plugins WHERE `element` = '".$info[2]."' AND `folder` = '".$info[1]."' ");
					$result = $db->loadObject();
					if( $result ){
						$modelFile = JPATH_ADMINISTRATOR.DS."components".DS."com_installer".DS."models".DS."plugins.php";
						if( file_exists($modelFile) ){
							require_once($modelFile);
							$model = new InstallerModelPlugins();
							$result = $model->remove($result->id);
							if($result)
								$messages[] = JText::_("Uninstall plugin: ".$package." successfully");
						}
					}
				}
				
				break;
			}
		}
	}
	
	function deleteFolder($path){
		$p_dir = JPath::clean( JPATH_SITE.DS.$path);
		if (is_dir($p_dir)){
			JFolder::delete($p_dir);
		}
	}
	
	function deleteDatabase($table, $condition){
		$db = JFactory::getDBO();
		$db->setQuery("DELETE FROM ".$table." WHERE ".$condition." ");
		$db->query();
	}
?>
 <div style="text-align:left;">
  	<table width="100%" border="0" style="line-height:200%; font-weight:bold;">	  
	    <tr>
	      <td align="center">
	      		<img src="../components/com_jajobboard/asset/images/jajobboard.png" />
	      		JA JobBoard and related modules, plugins is removed successfully!<br/>
				<?php echo implode("<br/>", $messages)?><br/>				
	      </td>
	    </tr>
    </table>
 </div>